package com.example.demo.controller;

import com.example.demo.pojo.ConfigProvince;
import com.example.demo.pojo.Student;
import com.example.demo.service.CrossProvinceService;
import com.example.demo.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Validated
@Slf4j
@RestController
@RequestMapping("/cross")
public class CrossProvince {

    @Autowired
    @Qualifier("asyncTaskExecutor1")
    private ThreadPoolTaskExecutor asyncTaskExecutor1;

    @Resource(name = "CrossProvinceService")
    private CrossProvinceService crossProvinceService;

    @PostMapping(value = "/sendGrade")
    public List<ResponseResult> sendGrade(@RequestBody HashMap<String,String> params) throws ExecutionException, InterruptedException {

        CompletableFuture<Student>  contentFuture =  CompletableFuture.supplyAsync(
                () -> crossProvinceService.getStudent(params.get("id")),asyncTaskExecutor1
        );

        CompletableFuture<List<ConfigProvince>> provinvcesFuture =  CompletableFuture.supplyAsync(
                () -> {
                    List<ConfigProvince> provinceList = crossProvinceService.getProvinceByIds(params.get("provinvces"));
                    provinceList.forEach(e->e.setUrl(e.getUrl()+"/evaluate/archives/shared"));
                    return provinceList;
                },asyncTaskExecutor1
        );

        CompletableFuture<List<ResponseResult>> resultFuture = contentFuture
                .thenCombine(provinvcesFuture, (content, provinceList) -> crossProvinceService.sendToProvinces(provinceList,content,params.get("id")));
        return resultFuture.get();

//        CompletableFuture<Void> allFutures =  CompletableFuture.allOf(tmpFuture, tmpFuture1);  //allOf组合所有异步任务，并使用join获取结果
//        allFutures.get();
//
//        List<EstimateDossierRecordShare>  content = tmpFuture.get();
//        List<ConfigProvince>  provinceList = tmpFuture1.get();
//        List<ResponseResult> list = crossProvinceService.sendToProvinces(provinceList,content,params.get("id"));
//        return list;
    }


    @PostMapping("/downloadFile")
    public List<ResponseResult> uploadEstimateFile(MultipartFile file,String privinces) throws IOException {
        List<ConfigProvince> provinceList = crossProvinceService.getProvinceByIds(privinces);
        provinceList.forEach(e->e.setUrl(e.getUrl()+"/evaluate/downloadFile"));
        ByteArrayResource fileAsResource = new ByteArrayResource(file.getBytes()) {
            @Override
            public String getFilename() {
                return file.getOriginalFilename();
            }
            @Override
            public long contentLength() {
                return file.getSize();
            }
        };
        return crossProvinceService.sendToProvinces(provinceList,fileAsResource,fileAsResource.getFilename());
    }

}
